<?php
	ini_set("include_path", ".:../:./includes:../includes");

	require_once("includes/constants.php");	
	require_once("includes/common.php");
	require_once("includes/template.php"); 
	require_once("includes/database.php");
	head("Bank Holidays"); 
?>

<?php 
	$_SESSION[BANK_HOLIDAYS] = getBankHolidays();
	$today = time();
	$year = strftime("%Y", $today);  //todo: make year selectable
	echo "<table cellspacing='15px' >";
	echo "<tr><td colspan='3' class='titleStyle'> $year </td></tr>";
	
	for ($month=1; $month <= 12; $month++) {
		if ($month == 1 || $month ==4 || $month == 7 || $month == 10) echo "<tr>";
		echo "<td valign='top' bgcolor='#E6E6FA'>";
		for ($day=1; $day <= 31; $day++) {
			$timestamp = mktime(0,0,0,$month,$day,$year);
			 
		
	 
			$dat = getdate($timestamp);	
			$found = false;
			if (isset($_SESSION[BANK_HOLIDAYS])) {
				foreach ($_SESSION[BANK_HOLIDAYS] as $bankHoliday) {
					if ($bankHoliday == $timestamp) {
						$found = true;
						break;
					}
				}
			}			
			echo "<input type=hidden name='" . 
					$timestamp . 
					"' id='" . 
					$timestamp .
					"'";
			if ($found)	{
				echo "value='$timestamp' ";
			}
			else {
				echo "value='' ";
			}	
			echo "  />";			
			if ($day == 1) { 
				echo "<table  ><tr><td colspan=7 align='center'> <b>" . strtoupper($dat['month']) . "</b></td></tr>";
				echo "<tr><td>&nbsp;Sun</td><td>&nbsp;Mon</td><td>&nbsp;Tue</td><td>&nbsp;Wed</td><td>&nbsp;Thu</td><td>&nbsp;Fri</td><td>&nbsp;Sat</td></tr>";
				if ($dat['wday'] > 0) echo "<tr>";
				for ($i=0; $i < $dat['wday']; $i++) {
						echo "<td>&nbsp;&nbsp;</td>";						 
				}
			}
			if ($dat['mon'] != $month) {
				$lastWeekDay =  $dat['wday'] - 1;
				if ($lastWeekDay == -1) $lastWeekDay = 6;
				for ($i=$lastWeekDay; $i < 6; $i++) {
						echo "<td>&nbsp;&nbsp;</td>";						
				}			
				break;
			}
			if ($dat['wday'] == 0) {
				echo "<tr>";
			}
			echo '<td onmouseover="javascript:this.className=';
			echo "'hoveredBankHoliday';";
			echo '"';
			
			if ($today < $timestamp) {	
				echo 'onmouseout="javascript:if (document.getElementById(' .
				"'$timestamp'" .  ").value == '' ) this.className=";
				echo "'unselectedCalendarDate';";
				echo '"';
			}
			else {
				echo 'onmouseout="javascript:if (document.getElementById(' .
				"'$timestamp'" .  ").value == '' ) this.className=";
				echo "'pastCalendarDate';";
				echo '"';				
			}
						

			if ($found) {
				echo " class='hoveredBankHoliday'";
			}
			else {
				if ($today < $timestamp) {			
					echo " class='unselectedCalendarDate'";
				}
				else {
					echo " class='pastCalendarDate'";
				}
			}
					
			if ($today < $timestamp) {		
				echo 'onclick="javascript:if (document.getElementById(' .
				"'$timestamp'" .  ").value == '' ) {document.getElementById('" . 
				$timestamp .  "').value = " .  "'$timestamp'" . ";this.className='hoveredBankHoliday'; } else { document.getElementById('"  .
				$timestamp .  "').value =''; this.className='unselectedCalendarDate'; }" .  '"';						
			}

			echo  '>';		
	
			echo "$day </td>";
			if ($dat['wday'] == 6) {
				echo "</tr>";
			}			
			
		}
		echo "</table>";
		echo "</td>";
		if ($month == 3 || $month ==6 || $month == 9 || $month == 12) echo "</tr>";
	}
	echo "</table>";

?>
<?php foot("SAVE", "bankHolidayLogic.php"); ?>